const express = require('express');
const app = express();
app.use(express.static("www"));
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }))
const mysql = require("mysql");
const mysqli = mysql.createConnection({
    host: "localhost", //ip
    user: "root",  //用户名
    password: "root", //密码
    database: "yuedu" //要连接的数据库名称
})
mysqli.connect(function (err) {
    if (err) {
        console.log("连接数据库失败");
    } else {
        console.log("连接数据库成功");
    }
})
app.get("/getOne", function (req, res) {
    var sql2 = "select * from t_types group by(id)";
    mysqli.query(sql2, function (err, data) {
        if (err) {
            console.log('表单1获取失败');
        } else {
            res.send(data);
        }
    })
})
app.get("/getTwo", function (req, res) {
    console.log(req.query);
    var sql = "select * from t_articles";
    mysqli.query(sql, function (err, data) {
        if (err) {
            console.log('表单2获取失败');
        } else {
            console.log('表单2获取成功');
            var data1 = [];
            for (let i = 0; i < data.length; i++) {
                if (req.query.id == data[i].type_id)
                    data1.push(data[i])
            }
            var data2 = [];
            for (let j = 0; j < data1.length; j++) {
                if (j >= req.query.page * 50 - 50 && j < req.query.page * 50) {
                    data2.push(data1[j]);
                    console.log(data2.length);
                }
                // console.log([data1,data2]);

            }
            res.send([data1, data2]);
        }
    })
})




app.listen(3001, () => {
    // 服务器运行成功后的回调函数
    console.log("http://localhost:3001");
})